<?php

declare (strict_types = 1);

namespace app\admin\model; 

use think\Model;

class Substation extends Model
{
    /**
     * 获取站点模块列表
     * @param $tag
     * @return array|mixed|object|\think\App
     */
    public function getList($tag) 
    {
        $tag['order'] = !empty($tag['order']) ? $tag['order'] : 'weigh desc';

        // 缓存设置
        $cacheTime = !empty($tag['cache']) && is_numeric($tag['cache']) ? intval($tag['cache']) : 3600;
        // 语言切换

        $tag['lang'] = site('content_lang_on')==1?app()->lang->getLangset():site('content_lang');
		
        $cacheID = to_guid_string($tag);
        if (!env('APP_DEBUG') && $cacheData = cache($cacheID)) {
            return $cacheData;
        }
		
        $map = [['status','=','normal'],['lang','=',$tag['lang']]];
		
        if (!empty($tag['parent_id'])) {
			$map[] = ['parent_id', '=', $tag['parent_id']];
		}
		
		if (!empty($tag['new_window'])) {
			$map[] = ['new_window', '=', $tag['new_window']];
		}
		
        $obj = $this->where($map)->where($tag['where'])->order($tag['order'])->select();
        if ($obj->isEmpty()) {
            return [];
        }
        $array = $obj->hidden(['delete_time'])->toArray();


        // 结果进行缓存
        if (!env('APP_DEBUG')) {
            cache($cacheID, $array, $cacheTime);
        }
        return $array;
		
    }
	
	/**
     * 设置多语言
     * @param $value
     * @param $data
     * @return mixed|string
     */
    public function setLangAttr($value, $data)
    {
        // 写入前，必须存入当前编辑模式的缓存
        return $value?$value:app()->cache->get('admin_content_lang'.app('user')->id);
        //if (site('content_lang_on')==1) {
        //    return $value?$value:app()->cache->get('admin_content_lang'.app('user')->id);
        //} else {
        //    return ''; // 没开启多语言，默认值空，
        //}
    }

    /**
     * 新增事件
     * @param Model $model
     */
    public static function onAfterInsert($model)
    {
        //if (site('content_lang_on')==1) {
        //    $data = $model->getData();
        //    $data['update_time'] = time();
        //    $data['create_time'] = time();
        //    lang_content_add('substation', $data, ['remark']);
        //}
    }

    /**
     * 删除的操作
     * @param Model $model
     */
    public static function onAfterDelete($model)
    {
        // 获取当前删除的ID
        $curId = $model->getAttr('id');
        if (site('content_lang_on')==1) {
            $data = lang_content_del('substation', $curId);
        }
    }
}
